package com.efuture.androidmvpdemo.login

import com.efuture.androidmvpdemo.net.ApiParameter
import com.efuture.androidmvpdemo.net.NetService
import com.efuture.netrequestdemo.bean.User
import com.efuture.netrequestdemo.utils.AppUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**

 *@description:TODO
 * @author: zhaoshanshan
 * @date: 2022/3/26 4:28 下午

 */
class LoginInteractor {
    interface OnLoginFinishedListener {

        fun onFail(reason:String)
        fun onSuccess()
    }

    fun login(mainScope:CoroutineScope, username: String, password: String, listener: OnLoginFinishedListener) {

        if(username.isEmpty()){
            listener.onFail("用户名不能为空")
            return
        }
        if(password.isEmpty()){
            listener.onFail("密码名不能为空")
            return
        }
        //开始调用网络
        mainScope.launch(Dispatchers.Main) {
            //登录
            val loginUser: User? = NetService.request(ApiParameter.loginUser(username,password),"正在登录中...")
            if(null!=loginUser&&loginUser.errorCode==0){
                listener.onSuccess()
            }else{
                if(null!=loginUser?.errorMsg){
                    listener.onFail(loginUser.errorMsg)

                }else{
                    listener.onFail("登录失败")
                }
            }
        }
    }
}